using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class IfParameter : MonoBehaviour
{
    public IfItem m_IfItemPrefab;
    public Text m_OrPrefab;
    public GameObject m_IfItemsParent;
    public List<IfItem> m_IfItems = new List<IfItem>();
    List<Text> m_Ors = new List<Text>();
    ConditionItem m_ConditionItem;
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    public void InitIfParameter(ConditionItem conditionItem)
    {
        foreach(var i in m_IfItems)
        {
            Destroy(i.gameObject);
        }
        foreach(var i in m_Ors)
        {
            Destroy(i.gameObject);
        }
        m_IfItems.Clear();
        m_Ors.Clear();
        m_ConditionItem = conditionItem;
        var triggers = conditionItem.m_ShipEvent.m_Events.Key;
        if(triggers != null)
            foreach (var triggerOr in triggers)
            {
                    var ifItem = AddItem();
                    ifItem.Init(triggerOr);
            }
    }
    public void OnAddIfItemCliced()
    {
        if (m_ConditionItem == null)
            return;
        AddItem();
    }

    IfItem AddItem()
    {

        var ifItem = Instantiate(m_IfItemPrefab);
        ifItem.gameObject.transform.SetParent(m_IfItemsParent.transform);
        ifItem.transform.SetSiblingIndex(Mathf.Max(m_IfItemsParent.transform.childCount - 3, 0));
        m_IfItems.Add(ifItem);
        var or = Instantiate(m_OrPrefab);
        or.gameObject.transform.SetParent(m_IfItemsParent.transform);
        or.transform.SetSiblingIndex(Mathf.Max(m_IfItemsParent.transform.childCount - 3, 0));
        m_Ors.Add(or);
        return ifItem;
    }
}
